$(function() {
	getWorkerType();//工种
	// 回填
	if (sex == 0) {
		$("input[name=sex][value='0']").attr("checked", true);
	} else {
		$("input[name=sex][value='1']").attr("checked", true);
	}

	$("#LAY_demo_upload").attr("src", ctp + '/' + photo);

	$("#edudegree").val(edudegree);
	$("#worktype").val(worktype);
	$("#chargetype").val(chargetype);

	$("input[name='payform']:radio").each(function() {
		if ($(this).val() == payform) {
			$(this).attr("checked", true);
		}
	});

	// tab切换
	tabStyleTurn('.smart-tab-item');
	tabSwitch('.smart-tab-item');
	$('div[id^=div_]').hide();
	$('#div_salary').show();

	// 合同回填
	getContractList(recId);
	selectGroups();

	// 表单渲染
	$('#form1').submit(function() {
		var options = {
			url : ctp + "/workerController/workerSave.shtml",
			type : 'POST',
			error : function(result) {
				jalert("保存失败，请核实闸机系统服务是否启动！");
			},
			success : function(result) {
				if (result == '1') {
					jalert9("保存成功！");
					setTimeout(function() {
						// parent.searchData();
						closeW();
					}, WARN_TIME_OUT);
				} else if (result == '0') {
					jalert("保存失败，请联系管理员！");
				} else if (result == '2') {
					jalert("工号已存在，请更换！");
				} else if (result == '3') {
					jalert("手机号已存在，请更换！");
				} else {
					jalert("保存失败，请核实闸机系统服务是否启动以及工人照片是否是人脸！");
				}
			}
		};
		$('#form1').ajaxSubmit(options);
		return false;
	});

});


function getWorkerType() {
	$.ajax({
		url : ctp + "/sysdictController/getDictListByType.shtml",
		data : {
			dicType : 'WORKER_TP'
		},
		type : "POST",
		cache : false,
		async: false,
		dataType : "json",
		success : function(data) {
			var list = data.list;
			var $str = '';
			$str += '<option value="">--请选择--</option>';
			for (var i = 0; i < list.length; i++) {
				var l = list[i];
				$str += '<option value="' + l.rec_value + '">' + l.rec_value + '</option>';
			}
			$('#worktype').html($str);
		}
	});
}

function selectGroups() {
	$.ajax({
		url : ctp + "/groupController/selectGroups.shtml",
		data : {},
		type : "POST",
		cache : false,
		dataType : "json",
		success : function(data) {
			var list = data.list;
			var $str = '';
			$str += '<select class="smart-body_select" name="groupid" id="groupid">';
			$str += '<option value="">--请选择--</option>';
			for (var i = 0; i < list.length; i++) {
				var l = list[i];
				$str += '<option value="' + l.REC_ID + '">' + l.NAME + '</option>';
			}
			$str += '</select>';
			$('#group').append($str);
			$('#groupid').val(groupId);

		}
	});
}

function uploadFile() {
	// alert(document.getElementById("contract").files[0].name);
	// alert(document.getElementById("contract").files[1].name);
	// alert(document.getElementById("contract").files[2].name);
	var files = document.getElementById("contract").files;
	var fileStr = "";
	for (var i = 0; i < files.length; i++) {
		fileStr += files[i].name + ',';
	}
	$('#file-name').html(fileStr);
}

// 合同回填
function getContractList(recId) {
	$.ajax({
		url : ctp + "/workerController/getWorkerContracts.shtml",
		data : {
			recId : recId
		},
		type : 'POST',
		async : false,
		dataType : "json",
		success : function(result) {
			var list = result.list;
			var html = "";
			if(list.length>0){
				for (var i = 0; i < list.length; i++) {
					html += '<div class="smart-layer_carousel" id="' + list[i].rec_id + '">' + '<img src="' + ctp + '/' + list[i].path + '" layui-src="' + ctp + '/' + list[i].path
							+ '">' + '<p></div>';
				};
				
				/*html += '<div class="smart-layer_carousel" id="' + list[i].rec_id + '">' + '<img src="' + ctp + '/' + list[i].path + '" layui-src="' + ctp + '/' + list[i].path
				+ '">' + '<p><a onclick="delContractByid(\'' + list[i].rec_id + '\');">删除</a><a onclick="downContract(\'' + list[i].path + '\',\'' + list[i].name
				+ '\');">下载</a>' + '</div>';*/
				$('#contractList').html(html);
				layui.layer.photos({
					photos : '#contractList',
					anim : 0
				});
			}else{
				$('#contractList').html('<div style="color:red;padding: 9px 0;">当前工人无合同配置，您可前往工人合同管理模块配置！</div>');
			}
			
		}
	});
}

function downContract(path, fileName) {
	location.href = ctp + "/pubController/downloadFile.shtml?path=" + path + "&fileName=" + fileName;
}

function delContractByid(recId) {
	jconfirm("del('" + recId + "')", "确定要删除所选的文件吗？");
}

function del(recId) {
	$.ajax({
		url : ctp + "/workerController/workerContractDel.shtml",
		data : {
			recId : recId
		},
		type : 'POST',
		async : false,
		// dataType: "json",
		success : function(result) {
			if (result == '') {
				$('#' + recId).remove();
				jalert9("删除成功！");
			} else {
				jalert(msg);
			}
		}
	});
}

function tabSwitch(tabclass) {
	var recId = $('#recId').val();
	$(tabclass).click(function(e) {
		if ("tab_salary" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_salary").show();
		} else if ("tab_jcqk" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_jcqk").show();
			getJcqk(recId);
		} else if ("tab_project" == $(this).attr("id")) {
			$('div[id^=div_]').hide();
			$("#div_project").show();
			getProject(recId);
		}

	});
}

// ajax 实现文件上传
function ajaxFileUpload() {
	$.ajaxFileUpload({
		url : ctp + "/workerController/photoUpload.shtml",
		secureuri : false,
		data : {
			p : new Date()
		},
		fileElementId : "photo",
		dataType : "text",
		success : function(data) {
			$('#LAY_demo_upload').attr("src", ctp + "/" + data);// 回显图片
			$('#photoPath').val(data);// 设置路径
		},
		error : function(data) {
			// 上传失败
		}
	});

}

function workerAdd() {
	setTagAndValue("form1","areaLName",$('#areaCode').val());
	setTagAndValue("form1","areaCode",$('#areaCode').attr("data-code"));
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	$('#form1').submit();

}

/** **********************************获取当前用户工程列表开始******************************************************************** */

var projectId="";

function getProject(recId) {
	projectId = recId;
	searchData2();
}

function searchData2(curr) {
	setTagAndValue("form3","recId",projectId);
	// ---开始的页数
	 setTagAndValue("form3","curr",curr);
	// ---每页几条数据
	setTagAndValue("form3", "SUNNADA_PAGE_SIZE", 6);
	getProjectInfo();
}

function getProjectInfo() {
	$.ajax({
		url : ctp + "/workerController/workerProjectSearch.shtml",
		data : $('#form3').serialize(),
		type : "POST",
		cache : false,
		dataType : "json",
		error : function(request) {
			closeLoading();
			alert("表单提交出错，请稍候再试");
		},
		success : function(data) {
			var dataObj = data.list;
			var pageJson = data.params;
			//$("#pNum").text(pageJson.rc);
			// ------数据拼接-------
			projectData(dataObj);
			// ------------ 分页开始 -------------
			 getPaging2(pageJson,dataObj,2,2);
			// ------------ 分页结束 -------------
		}

	});
}

function projectData(list) {
	$("#tbodyData").html("");
	if (list.length > 0) {
		for (var i = 0; i < list.length; i++) {
			var l = list[i];
			$str = "";
			$str += "<tr><td>" + l.NAME + "</td>";
			if (l.GROUPNAME == undefined) {
				$str += "<td>暂无班组</td>";
			} else {
				$str += "<td>" + l.GROUPNAME + "</td>";
			}

			$str += "<td>" + l.WORKTYPE + "</td>";
			$str += "<td>" + l.BGTIME + "</td>";
			$str += "<td>" + l.EDTIME + "</td>";
			$str += "<td>" + l.PROJECTSTATE + "</td></tr>";
			$("#tbodyData").append($str);
		}
	} else {
		var $str = '<tr><td colspan="7" align="center">暂无数据</td></tr>';
		$("#tbodyData").append($str);
	}

}
/** **********************************获取当前用户工程列表结束******************************************************************** */

/** **********************************获取当前用户奖惩情况列表开始******************************************************************** */

var JcqkId="";

function getJcqk(recId) {
	JcqkId = recId;
	searchData1();
}

function searchData1(curr) {
	setTagAndValue("form2","recId",JcqkId);
	// ---开始的页数
	 setTagAndValue("form2","curr",curr);
	// ---每页几条数据
	setTagAndValue("form2", "SUNNADA_PAGE_SIZE", 6);
	getJcqkInfo();
}

function getJcqkInfo() {
	$.ajax({
		url : ctp + "/workerController/workerJcqkSearch.shtml",
		data : $('#form2').serialize(),
		type : "POST",
		cache : false,
		dataType : "json",
		error : function(request) {
			closeLoading();
			alert("表单提交出错，请稍候再试");
		},
		success : function(data) {
			var dataObj = data.list;
			var pageJson = data.params;
			// $("#pNum").text(pageJson.rc);
			// ------数据拼接-------
			jcqkData(dataObj);
			// ------------ 分页开始 -------------
			getPaging2(pageJson,dataObj,1,1);
			// ------------ 分页结束 -------------
		}

	});
}

function jcqkData(list) {
	$("#tbodyJcqkData").html("");
	if (list.length > 0) {
		for (var i = 0; i < list.length; i++) {
			var l = list[i];
			$str = "";
			$str += "<tr><td>" + l.APPLY_TIME + "</td>";
			if (l.PROJECT == undefined) {
				$str += "<td></td>";
			} else {
				$str += "<td>" + l.PROJECT + "</td>";
			}
			$str += "<td>" + l.REWARD_TYPE + "</td>";
			$str += "<td>" + l.REWARD_MONEY + "</td>";
			$str += "<td>" + l.REWARD_LEVEL + "</td>";
			$str += "<td>" + l.REWARD_DESC + "</td>";
			$str += "<td>" + l.REWARD_RESULT + "</td>";
			$str += "<td>" + l.AUDIT_RESULT + "</td>";
			$str += "</tr>";
			$("#tbodyJcqkData").append($str);
		}
	} else {
		var $str = '<tr><td colspan="7" align="center">暂无数据</td></tr>';
		$("#tbodyJcqkData").append($str);
	}

}
/** **********************************获取当前用户奖惩情况列表结束******************************************************************** */

// 当前弹窗索引
var _index = parent.layui.layer.getFrameIndex(window.name);
function closeW() {
	// parent.layui.layer.close(_index);
	// location.href = ctp + "/workerController/worker.shtml";
	history.back();
}